package lanou.autohome.findcar.fragments;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.litesuits.orm.LiteOrm;

import java.util.ArrayList;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import lanou.autohome.BuildConfig;
import lanou.autohome.URLValues;
import lanou.autohome.basetool.BaseActivity;
import lanou.autohome.basetool.DBTool;
import lanou.autohome.basetool.NetTool;
import lanou.autohome.collection.CollectionBean;
import lanou.autohome.collection.CollectionLocalBean;
import lanou.autohome.findcar.beans.FilterItemBean;
import lanou.autohome.R;

/**
 * Created by dllo on 16/9/2.
 */
public class FilterItemActivity extends BaseActivity {

    private TextView name;
    private TextView fctprice;
    private TextView levelname;
    private ImageView logo;
    private ImageView back;
    private CheckBox checkBox;
    private CheckBox share;
    private String namec;
    private String fctpricec;
    private String logoc;
    private String levelnamec;
    //item的大bean
    private FilterItemBean filterItemBean;
    static LiteOrm liteOrm;


    @Override
    protected int setLayout() {
        return R.layout.activity_filter_onclick_item;
    }

    @Override
    protected void intiView() {
        //网络获取的数据
        name = bindView(R.id.name_filter_onclick_item);
        fctprice = bindView(R.id.fctprice_filter_onclick_item);
        levelname = bindView(R.id.levelname_filter_onclick_item);
        logo = bindView(R.id.logo_filter_onclick_item);
        //返回上一页按钮
        back = bindView(R.id.imageview_filter_onclick_back);
        back.setOnClickListener(this);
        //收藏按钮
        checkBox = bindView(R.id.checkbox_shoucang_filter_onclick_item);
        checkBox.setOnClickListener(this);
        //分享按钮
        share = bindView(R.id.btn_fenxiang_filter_onclick_item);
        share.setOnClickListener(this);

    }

    @Override
    protected void intiData() {
        Intent intent = getIntent();
        liteOrm = LiteOrm.newSingleInstance(this, "MyDataBase.db");
        String aa = intent.getStringExtra("aa");
        final NetTool mNetTool = new NetTool();
        mNetTool.getNetData(URLValues.SCREENING_IN_URL_START + aa + URLValues.SCREENING_IN_URL_END, FilterItemBean.class, new NetTool.NetListener<FilterItemBean>() {
            @Override
            public void onSuccess(FilterItemBean filterItemBean) {
                name.setText(filterItemBean.getResult().getName());
                fctprice.setText(filterItemBean.getResult().getFctprice());
                levelname.setText(filterItemBean.getResult().getLevelname());
                mNetTool.getImage(filterItemBean.getResult().getLogo(), logo);
                namec = filterItemBean.getResult().getName();
                fctpricec = filterItemBean.getResult().getFctprice();
                levelnamec = filterItemBean.getResult().getLevelname();
                logoc = filterItemBean.getResult().getLogo();
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageview_filter_onclick_back:
                finish();
                break;
            case R.id.checkbox_shoucang_filter_onclick_item:
                final BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null) {
                    if (checkBox.isChecked()) {
                        //存入本地数据库
                        DBTool.getInstance().set("users = ?", bmobUser.getUsername(), "name = ?", namec, new DBTool.QueryListener() {
                            @Override
                            public void onQueryComplete(ArrayList<CollectionLocalBean> localBeen) {
                                if (localBeen.size() == 0) {
                                    CollectionLocalBean collectionLocalBean = new CollectionLocalBean();
                                    collectionLocalBean.setUsers(bmobUser.getUsername());
                                    collectionLocalBean.setName(namec);
                                    collectionLocalBean.setFctprice(fctpricec);
                                    collectionLocalBean.setLevelname(levelnamec);
                                    collectionLocalBean.setLogo(logoc);
                                    DBTool.getInstance().insertUser(collectionLocalBean);
                                    checkBox.setBackgroundResource(R.mipmap.shoucanghuang);
                                    Toast.makeText(FilterItemActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                                    Log.d("FilterItemActivity", "collectionLocalBean:" + collectionLocalBean);
                                } else {
                                    //删除本地数据库
                                    liteOrm.delete(localBeen);
                                    checkBox.setBackgroundResource(R.mipmap.soucanglan);
                                    Toast.makeText(FilterItemActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        //存入网络数据库
                        CollectionBean collectionBean = new CollectionBean();
                        collectionBean.setName(bmobUser.getUsername());
                        collectionBean.setLevelname(namec);
                        collectionBean.setFctprice(fctpricec);
                        collectionBean.setLogo(logoc);
                        collectionBean.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null) {
                                    Log.d("FilterItemActivity", "收藏成功");
                                } else {
                                    if (BuildConfig.DEBUG)
                                        Log.d("FilterItemActivity", "收藏失败".toString());
                                }
                            }
                        });
                    }
                } else {
                    Toast.makeText(FilterItemActivity.this, "没收藏成功,请查看是否登录", Toast.LENGTH_SHORT).show();
                    Log.d("FilterItemActivity", "收藏失败 , 没有登录".toString());
                }
                break;
            case R.id.btn_fenxiang_filter_onclick_item:
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/*");
                share.putExtra(Intent.EXTRA_SUBJECT , "Share");
                share.putExtra(Intent.EXTRA_TEXT , "I have successfully share my message through my app");
                share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(Intent.createChooser(share , getTitle()));

        }
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }

}
